home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / term / hp500c.trm < prev    next >
Text File  |  1993-09-15  |  11KB  |  455 lines

  1. /*
  2.  * $Id: hp500c.trm%v 3.50.1.11 1993/08/10 03:55:03 woo Exp $
  3.  *
  4.  */
  5. /* GNUPLOT - hp500c.trm */
  6. /*
  7.  * Copyright (C) 1993
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software  is provided "as is" without express or implied warranty.
  20.  * 
  21.  * This file is included by ../term.c.
  22.  *
  23.  * This terminal driver supports:
  24.  *  hpdj 500c
  25.  *
  26.  * AUTHORS
  27.  *  John Engels      -- \
  28.  *  Russell Lang     ----> HPLJII.trm
  29.  *  Maurice Castro   -- /
  30.  *  UdoHessenauer    ----> derived this version from the above one
  31.  *
  32.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  33.  * 
  34.  */
  35.  
  36. /* The following HP Deskjet500c  driver uses generic bit mapped graphics
  37.    routines from bitmap.c to build up a bit map in memory.  The driver
  38.    interchanges colomns and lines in order to access entire lines
  39.    easily and returns the lines to get bits in the right order :
  40.    (x,y) -> (y,XMAX-1-x). */
  41. /* This interchange is done by calling b_makebitmap() with reversed 
  42.    xmax and ymax, and then setting b_rastermode to TRUE.  b_setpixel()
  43.    will then perform the interchange before each pixel is plotted */
  44. /* by John Engels JENGELS@BNANDP51.BITNET, inspired by the hpljet driver
  45.    of Jyrki Yli-Nokari */
  46.  
  47. #ifdef HP500C
  48.  
  49. /* We define 4 different print qualities : 300ppi, 150ppi, 100ppi and
  50.    75ppi.  (Pixel size = 1, 2, 3, 4 dots) */
  51.  
  52. #define HP500C_DPP (hpdj_dpp)   /* dots per pixel */
  53. #define HP500C_PPI (300/HP500C_DPP) /* pixel per inch */
  54. /* make XMAX and YMAX a multiple of 8 */
  55. #define HP500C_XMAX (8*(unsigned int)(xsize*1920/HP500C_DPP/8.0+0.9))
  56. #define HP500C_YMAX (8*(unsigned int)(ysize*1920/HP500C_DPP/8.0+0.9))
  57.  
  58. #define HP500C_VCHAR (HP500C_PPI/6) /* Courier font with 6 lines per inch */
  59. #define HP500C_HCHAR (HP500C_PPI/10) /* Courier font with 10 caracters
  60. per inch */
  61.  
  62. /* default values for term_tbl */
  63. #define HP500C_75PPI_XMAX (1920/4)
  64. #define HP500C_75PPI_YMAX (1920/4)
  65. #define HP500C_75PPI_HCHAR (1920/4/6)
  66. #define HP500C_75PPI_VCHAR (1920/4/10)
  67. #define HP500C_75PPI_VTIC 5
  68. #define HP500C_75PPI_HTIC 5
  69.  
  70.  
  71. #define HP500C_PUSH_CURSOR fprintf(outfile,"\033&f0S") /* Save current
  72. cursor position */
  73. #define HP500C_POP_CURSOR fprintf(outfile,"\033&f1S") /* Restore
  74. cursor position */
  75. #define HP500C_COURIER fprintf(outfile,"\033(0N\033(s0p10.0h12.0v0s0b3T\033&l6D")
  76. /* be sure to use courier font with 6lpi and 10cpi */
  77.  
  78. static int hpdj_dpp=4;
  79. static int HP_COMP_MODE = 0;
  80.  
  81. /* bm_pattern not appropriate for 300ppi graphics */
  82. #ifndef HPLJII
  83. static unsigned int b_300ppi_pattern[] = {
  84.   0xffff, 0x1111,
  85.   0xffff, 0x3333, 0x0f0f, 0x3f3f, 0x0fff, 0x00ff, 0x33ff};
  86. #endif
  87.  
  88.  
  89. HP500Coptions()
  90. {
  91.   char opt[6];
  92.   
  93. #define HPDJCERROR "expecting dots per inch size 75, 100, 150 or 300 and/or compression method"
  94.   while (!END_OF_COMMAND) {
  95.     if (token[c_token].length>4)
  96.       int_error(HPDJCERROR,c_token);
  97.     
  98.     /* almost_equals() won't accept numbers - use strcmp() instead */
  99.     capture(opt,c_token,c_token);
  100.     if (!strcmp(opt,"75")) {
  101.       hpdj_dpp = 4;
  102.       HP_COMP_MODE = 0;
  103.     
  104. }
  105.     else if (!strcmp(opt,"100")) {
  106.       hpdj_dpp = 3;
  107.       HP_COMP_MODE = 0;
  108.     }
  109.     else if (!strcmp(opt,"150")) {
  110.       hpdj_dpp = 2;
  111.       HP_COMP_MODE = 0;
  112.     }
  113.     else if (!strcmp(opt,"300")) {
  114.       hpdj_dpp = 1;
  115.       HP_COMP_MODE = 0;
  116.     }
  117.     else if (!strcmp(opt,"rle")){
  118.       HP_COMP_MODE = 1;
  119.     }
  120.     else if (!strcmp(opt,"tiff")){
  121.       HP_COMP_MODE = 2;
  122.     }
  123.     c_token++;
  124.   }
  125.   
  126.   term_tbl[term].xmax = HP500C_XMAX;
  127.   term_tbl[term].ymax = HP500C_YMAX;
  128.   switch(hpdj_dpp) {
  129.   case 1:
  130.     strcpy(term_options,"300");
  131.     term_tbl[term].v_tic = 15;
  132.     term_tbl[term].h_tic = 15;
  133.     break;
  134.   case 2:
  135.     strcpy(term_options,"150");
  136.     term_tbl[term].v_tic = 8;
  137.     term_tbl[term].h_tic = 8;
  138.     break;
  139.   case 3:
  140.     strcpy(term_options,"100");
  141.     term_tbl[term].v_tic = 6;
  142.     term_tbl[term].h_tic = 6;
  143.     break;
  144.   case 4:
  145.     strcpy(term_options,"75");
  146.     term_tbl[term].v_tic = 5;
  147.     term_tbl[term].h_tic = 5;
  148.     break; 
  149.   }
  150.   switch(HP_COMP_MODE) {
  151.   case 0:
  152.     strcat(term_options," no comp");
  153.     break;
  154.   case 1:
  155.     strcat(term_options," RLE");
  156.     break;
  157.   case 2:
  158.     strcat(term_options," TIFF");
  159.     break;
  160.   case 3:     /* not implemented yet */
  161.     strcat(term_options," Delta Row");
  162.     break;
  163.   }
  164. }
  165.  
  166. HP500Cinit()
  167. {
  168. #ifdef vms
  169.     reopen_binary();
  170. #endif /* vms */
  171. #ifdef PC
  172.     reopen_binary();
  173. #endif /* PC */
  174. }
  175.  
  176. HP500Creset()
  177. {
  178. #ifdef vms
  179.     fflush_binary();
  180. #endif
  181. }
  182.  
  183.  
  184.  
  185. /* HP DeskJet 500c routines */
  186.  
  187. HP500Clinetype(linetype)
  188. int linetype;
  189. {
  190.     if (linetype < 0)
  191.         linetype = 7;
  192.     else if (linetype >=8){
  193.         linetype %=8;
  194.     }
  195.     switch (linetype){
  196.     case 0 :
  197.         linetype = 6;
  198.         break;
  199.     case 1 :
  200.         linetype = 5;
  201.         break;
  202.     case 2 :
  203.         linetype = 3;
  204.         break;
  205.     case 3 :
  206.         linetype = 2;
  207.         break;
  208.     case 4 :
  209.         linetype = 1;
  210.         break;
  211.     case 5 :
  212.         linetype = 4;
  213.         break;
  214.     case 6 :
  215.         linetype = 7;
  216.     }
  217.     b_setvalue(linetype);
  218.  
  219. }
  220.  
  221. /*
  222. HP500Cpoint(x,y,value)
  223. int x;
  224. int y;
  225. int value;
  226. {
  227.      HP500Clinetype(value);
  228.      do_point(x,y,value);
  229. }
  230. */
  231.  
  232. HP500Cgraphics()
  233. {
  234.     switch(hpdj_dpp) {
  235.     case 1:
  236.         b_charsize(FNT13X25);
  237.         term_tbl[term].v_char = FNT13X25_VCHAR;
  238.         term_tbl[term].h_char = FNT13X25_HCHAR;
  239.         break;
  240.     case 2:
  241.         b_charsize(FNT13X25);
  242.         term_tbl[term].v_char = FNT13X25_VCHAR;
  243.         term_tbl[term].h_char = FNT13X25_HCHAR;
  244.         break;
  245.     case 3:
  246.         b_charsize(FNT9X17);
  247.         term_tbl[term].v_char = FNT9X17_VCHAR;
  248.         term_tbl[term].h_char = FNT9X17_HCHAR;
  249.         break;
  250.     case 4:
  251.         b_charsize(FNT5X9);
  252.         term_tbl[term].v_char = FNT5X9_VCHAR;
  253.         term_tbl[term].h_char = FNT5X9_HCHAR;
  254.         break;
  255.     }
  256.     /* rotate plot -90 degrees by reversing XMAX and YMAX and by 
  257.     setting b_rastermode to TRUE */
  258.     b_makebitmap(HP500C_YMAX,HP500C_XMAX,3);
  259.     b_rastermode = TRUE;
  260. }
  261. /*
  262.  * Run-length encoding for the DeskJet. We have pairs of <count>
  263.  * <what>, where count goes from 0 (meaning one count) to 255
  264.  * this might double the size of the image.
  265.  */
  266.  
  267. static int  compress(op, oe, cp)
  268. unsigned char *op, *oe, *cp;
  269. {
  270.     unsigned char *ce = cp;
  271.     while ( op < oe ) {
  272.         unsigned char prevchar;
  273.         unsigned char count;
  274.  
  275.         prevchar = *op;     /* remember char */
  276.         count = 1;          /* its read the first time */
  277.  
  278.         while ( ++op < oe && *op == prevchar && count < 255){  /* set op to the next char */
  279.             count++;                                             /* and count it  */
  280.         }
  281.         *ce++ = --count; /* were ready, so correct the count */
  282.         *ce++ = prevchar; /* and store <what> */
  283.     }
  284.     *ce = 0;          /* just to be safe   */
  285.     return ce - cp;    /* length of  cbufs */
  286. }
  287.  
  288. unsigned char complement(c)
  289. int c;
  290. {
  291.     return (unsigned char)(256 - c);
  292. }
  293.  
  294.  
  295. static int compress_to_TIFF(op,oe,cp)
  296.      unsigned char *op;     /* original pointer */
  297.      unsigned char *oe;     /* end of orig string */
  298.      unsigned char *cp;     /* pointer for compressed data */
  299. {
  300.   unsigned char *countposition;
  301.   unsigned char *ce = cp;
  302.   while ( op < oe ) {
  303.     unsigned char prevchar;
  304.     unsigned char count;
  305.     
  306.     prevchar = *op;     /* gelesenes Zeichen aufbewaren */
  307.     count = 1;          /* bisher wurde es einmal gelesen */
  308.     
  309.     while ( ++op < oe && *op == prevchar && count < 128){  
  310.       count++;                                           
  311.     }
  312.     *ce = complement(count-1);
  313.     countposition = ce++;           /* remember count for building blocks of literal bytes */
  314.     *ce++ = prevchar;           
  315.     
  316.     if (count < 2) {      
  317.       while(op < oe && (prevchar!=*op||*op != *(op + 1))){/* only use rle for at leat 3 equal bytes */
  318.     
  319.     *ce++ = *op;                                       
  320.     count++;                                         
  321.     prevchar = *op++;                              
  322.     if (op > oe)
  323.       puts("FATAL op> oe!!\n");
  324.       }
  325.       if (op < oe && prevchar == *op){                             
  326.     op--;                                           
  327.     count--;                                       
  328.     ce--;                                         
  329.       }
  330.       
  331.       *countposition = count-1;                           
  332.     }
  333.   }
  334.   return ce - cp;  
  335.   
  336. }
  337.  
  338. static int
  339. nocompress(op,oe,cp)
  340.      unsigned char *op;
  341.      unsigned char *oe;
  342.      unsigned char *cp;
  343. {
  344.   unsigned char *ce = cp;
  345.   while(op < oe)
  346.     *ce++ = *op++;
  347.   return ce - cp;
  348. }
  349.  
  350. /* 0 compression raster bitmap dump. Compatible with HP DeskJet 500
  351.    hopefully compatible with other HP Deskjet printers */
  352.  
  353. HP500Ctext()
  354. {
  355.   register int x,j,row,count;
  356.   char *obuf,*oe,*cbuf,*ce;
  357.   
  358.   if((obuf = (char *) malloc(100*b_psize))==0)
  359.     puts("FATAL!-- couldn't get enough memory for obuf");
  360.   if((cbuf = (char *) malloc(400*b_psize))==0)
  361.     puts("FATAL!-- couldn't get enough memory for cbuf");
  362.   
  363.   oe = obuf;
  364.   
  365.   fprintf(outfile,"\033*t%dR", HP500C_PPI);
  366.   fprintf(outfile, "\033*r1A");
  367.   fprintf(outfile,"\033*b%1dM",HP_COMP_MODE);
  368.   fprintf(outfile, "\033*r%dS",b_ysize);
  369.   fprintf(outfile,"\033*r-3U");
  370.   
  371.   
  372.   /* dump bitmap in raster mode */
  373.   for (x = b_xsize-1; x >= 0; x--) {
  374.     row = (b_ysize/8)-1;
  375.     for (j = row; j >= 0; j-- ) {
  376.       *oe++ =  (char)(*((*b_p)[j]+x));    
  377.     }
  378.     switch (HP_COMP_MODE){
  379.     case 2 :
  380.       count = compress_to_TIFF(obuf,oe,cbuf);
  381.       break;
  382.     case 1 :
  383.       count = compress(obuf,oe,cbuf);
  384.       break;
  385.     case 0 :
  386.       count = nocompress(obuf,oe,cbuf);
  387.       break;
  388.     }
  389.     fprintf(outfile, "\033*b%dV", count);
  390.     ce = cbuf;
  391.     while(count--)
  392.       fputc(*ce++,outfile);
  393.     oe = obuf;
  394.     
  395.     for (j = row; j >=0; j-- ) {
  396.       *oe++ = (char)(*((*b_p)[j+b_psize]+x));
  397.     }
  398.     switch (HP_COMP_MODE){
  399.     case 2 :
  400.       count = compress_to_TIFF(obuf,oe,cbuf);
  401.       break;
  402.     case 1 :
  403.       count = compress(obuf,oe,cbuf);
  404.       break;
  405.     case 0 :
  406.       count = nocompress(obuf,oe,cbuf);
  407.       break;
  408.       
  409.     }
  410.     
  411.     fprintf(outfile, "\033*b%dV", count);
  412.     ce = cbuf;
  413.     while(count--)
  414.       fputc(*ce++,outfile);
  415.     oe = obuf;
  416.     
  417.     for (j = row; j >=0; j-- ) {
  418.       *oe++ =  (char)(*((*b_p)[j+(2*b_psize)]+x));
  419.     }
  420.     switch (HP_COMP_MODE){
  421.     case 2 :
  422.       count = compress_to_TIFF(obuf,oe,cbuf);
  423.       break;
  424.     case 1 :
  425.       count = compress(obuf,oe,cbuf);
  426.       break;
  427.     case 0 :
  428.       count = nocompress(obuf,oe,cbuf);
  429.       break;
  430.     }
  431.     fprintf(outfile, "\033*b%dW", count);
  432.     ce = cbuf;
  433.     while(count--)
  434.       fputc(*ce++,outfile);
  435.     oe = obuf;
  436.     
  437.   }
  438.   fprintf(outfile, "\033*rbC");
  439.   free(cbuf);
  440.   free(obuf);  
  441.   b_freebitmap();
  442.   
  443. #ifndef vms  /* most vms spoolers add a formfeed character */
  444.   fprintf(outfile,"\f");
  445. #endif /* not vms */
  446. }
  447.  
  448. #define HP500Ctext_angle b_text_angle
  449. #define HP500Cput_text b_put_text
  450. #define HP500Cmove b_move
  451. #define HP500Cvector b_vector
  452.  
  453. #endif /* HP500C */
  454.  
  455.